package org.aeon.gamechatnest.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aeon.gamechatnest.common.response.Result;
import org.aeon.gamechatnest.common.response.ResultBuild;
import org.aeon.gamechatnest.dto.UserVo;
import org.aeon.gamechatnest.pojo.ServerMembers;
import org.aeon.gamechatnest.service.ServerMembersService;
import org.aeon.gamechatnest.util.SecurityUtil;
import org.aeon.gamechatnest.validated.Add;
import org.aeon.gamechatnest.validated.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Liys 2024/5/13 16:51
 */
@Validated
@RestController
@RequestMapping("/server/members")
@Tag(name = "ServerMembersControllerApi", description = "用户服务器成员控制器")
public class ServerMembersController {
    @Autowired
    private ServerMembersService serverMembersService;

    // TODO　加入服务器邀请连接判定
    @Operation(summary = "当前登录用户加入服务器")
    @PostMapping
    public Result join(@RequestBody @Validated(Add.class) ServerMembers serverMembers) {
        serverMembers.setUserId(SecurityUtil.getUserId());
        serverMembers.setServerId(SecurityUtil.getServer());
        boolean result = serverMembersService.join(serverMembers);
        return ResultBuild.successIf(result).build();
    }

    @PreAuthorize("hasAnyAuthority('server-members:kick','admin','creator') " +
            "OR #userId==T(org.aeon.gamechatnest.util.SecurityUtil).userId")
    @Operation(summary = "成员被踢出服务器,userId为自己时为主动退出")
    @DeleteMapping("/{userId}")
    public Result kickOutServer(@PathVariable("userId") Long userId) {
        Long serverId = SecurityUtil.getServer();
        boolean result = serverMembersService.removeById(serverId, userId);
        return ResultBuild.successIf(result).build();
    }


    @PreAuthorize("hasAnyAuthority('server-members:modify-note-name','admin','creator') " +
            "OR #serverMembers.userId==T(org.aeon.gamechatnest.util.SecurityUtil).userId")
    @Operation(summary = "修改服务器下用户的备注名")
    @PutMapping
    public Result modifyNoteName(@RequestBody @Validated(Update.class) ServerMembers serverMembers) {
        // 获取当前服务器
        serverMembers.setServerId(SecurityUtil.getServer());
        serverMembers.setUserId(SecurityUtil.getUserId());
        // 修改备注名
        boolean result = serverMembersService.modifyNoteName(serverMembers);
        return ResultBuild.successIf(result).build();
    }

    @PreAuthorize("hasAnyAuthority('server-members:all','admin','creator')")
    @Operation(summary = "查询当前服务器下全部成员")
    @GetMapping("/all")
    public Result findByServerId() {
        Long serverId = SecurityUtil.getServer();
        List<UserVo> result = serverMembersService.getAllByServerId(serverId);
        return ResultBuild.result().successful().data(result).build();
    }

    @PreAuthorize("hasAnyAuthority('server-members:online','admin','creator')")
    @Operation(summary = "查询指定服务器下在线成员")
    @GetMapping("/online")
    public Result findOnLineByServerId() {
        Long serverId = SecurityUtil.getServer();
        List<UserVo> result = serverMembersService.getOnLineByServerId(serverId);
        return ResultBuild.result().successful().data(result).build();
    }
}
